package com.example.blog.controller;

import com.example.blog.common.R;
import com.example.blog.dao.ArticleDAO;
import com.example.blog.dao.CommentDAO;
import com.example.blog.dao.UserDAO;
import com.example.blog.dto.CommentDTO;
import com.example.blog.model.Article;
import com.example.blog.model.Comment;
import com.example.blog.model.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    public UserDAO userDAO;
    @Resource
    public CommentDAO commentDAO;
    @Resource
    public ArticleDAO articleDAO;

    @PostMapping("/add")
    public R addComment(@RequestBody CommentDTO commentDTO) {
        User user = userDAO.findById(commentDTO.userId).get();
        Article article = articleDAO.findById(commentDTO.articleId).get();

        Comment comment = new Comment(commentDTO.content, user, article);
        commentDAO.save(comment);
        return R.success();
    }
}
